package com.hd.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hd.dao.BaseDao;
import com.hd.entity.Attach;
import com.hd.entity.Notice;
import com.hd.entity.NoticeReaded;
import com.hd.service.NoticeService;
import com.hd.util.StringUtils;
import com.hd.util.result.PageInfo;
/**
* 通知公告，最新动态
*/
@Service("noticeService")
public class NoticeServiceImpl implements NoticeService {
    @Autowired
    private BaseDao<Notice,String>noticeDao;
    @Autowired
    private BaseDao<Attach,String> attachDao;
    @Autowired
    private BaseDao<NoticeReaded,String> noticeReadedDao;
    @Override
    public void save(Notice notice) {
		notice.setId(UUID.randomUUID().toString());
		notice.setCreateTime(new Date());
       noticeDao.save(notice);
    }

    @Override
    public void delete(String id){
      noticeDao.delete(Notice.class,id);
    }
       

    @Override
    public void update(Notice notice) {
        noticeDao.update(notice);
    }
    @Override
    public Notice get(String id){
    	Notice notice = noticeDao.get(Notice.class, id);
    	if(StringUtils.isNotNull(notice.getAttachIds())) {
    		String hql="from Attach where id in("+StringUtils.convertSingleStr(notice.getAttachIds())+")";
    		List<Attach>attachList=attachDao.find(hql);
    		notice.setAttachs(attachList);
    	}
    	return notice;
    }
        

    @Override
    public void selectDataGrid(PageInfo pageInfo) {
        String hql="from Notice where 1=1 ";
        Map<String,Object>condition=pageInfo.getCondition();
        List<Object> params=new ArrayList<Object>();
        if(StringUtils.isNotNull(condition.get("title"))) {
        	hql+=" and title like ?";
        	params.add("%"+condition.get("title")+"%");
        }
        if(StringUtils.isNotNull(condition.get("type"))) {
        	hql+=" and type=?";
        	params.add(condition.get("type"));
        }
        
        if(StringUtils.isNotNull(condition.get("userid"))) {
        	hql+=" and userid=?";
        	params.add(condition.get("userid"));
        }
        hql+=" order by createTime desc";
        String countSql="select count(*) "+hql;
        List<Notice> notices=noticeDao.findPage(hql, pageInfo.getNowpage(), pageInfo.getPagesize(),params);
        if(notices.size()>0) {
        	for(Notice notice:notices) {
        		if(StringUtils.isNotNull(notice.getAttachIds())) {
            		String hql1="from Attach where id in("+StringUtils.convertSingleStr(notice.getAttachIds())+")";
            		List<Attach>attachList=attachDao.find(hql1);
            		notice.setAttachs(attachList);
            	}
        		notice.setReaded(getReaded(notice, (String)condition.get("currentUserid")));
        	}
        }
        pageInfo.setRows(notices);
        Long total=noticeDao.getCountByList(countSql, params);
        pageInfo.setTotal(Integer.parseInt(total.toString()));
    }

    @Override
    public Integer getReaded(Notice notice,String currentUserid){
    	Integer readed=0;
    	String hql="from NoticeReaded where noticeId=? and userid=?";
    	List<NoticeReaded>noticeReadeds=noticeReadedDao.find(hql, notice.getId(),currentUserid);
    	if(noticeReadeds.size()>0){
    		readed=1;
    	}
    	return readed;
    }

	@Override
	public Long getNonReadedCount(String userid,String type) {
		String countSql1="select count(*) from Notice where type=?";
		Long total1=noticeDao.getCount(countSql1,type);
		String countSql2="select count(*) from NoticeReaded where type=? and userid=?";
		Long total2=noticeReadedDao.getCount(countSql2,type,userid);
		return total1-total2;
	}


}
